package module.building.view.buildingListPanel
{
	import code.consts.FilterConst;
	
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinUI.containers.Box;
	import com.darwin.darwinUI.controls.IListItem;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.core.UISprite;
	import com.darwin.darwinUI.general.DarwinFont;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.ToolTipManager;
	import com.darwin.darwinUI.manager.UIManager;
	import com.darwin.darwinUI.uiconst.UIAlign;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import module.building.model.dataObj.BuildingObj;
	import module.building.view.buildingTips.BuildingTips;
	import module.building.view.panelUtils.IconLabel;
	import module.main.model.dataObj.UserInfoObj;
	import module.main.view.ResourceConst;
	
//	import mx.effects.Tween;

	/**
	 * 建筑列表的单个建筑项 
	 * @author hc
	 */
	public class BuildListItem extends UIComponent implements IListItem
	{
		private var _selected:Boolean;
		private var _data:Object;
		private var _index:int;
		private var _resourceEnoughArray:Array = [true,true,true,true];
		private var _resourceEnough:Boolean = true;
		private var _goldEnough:Boolean = true;
		
		private var image:UISprite;
		private var goldIcon:UIComponent;
		private var goldLabel:Label;
		private var goldContainer:UIComponent;
		private var metirialBox:Box;
		private var stoneLabel:IconLabel;
		private var woodLabel:IconLabel;
		private var ironLabel:IconLabel;
		private var nameLabel:Label;
		private var numberLabel:Label;
		private var _resourceTipsContainer:Box;
		private var _resourceTips:UISprite;
		
		
		public function BuildListItem()
		{
			super();
			initView();
			initData();
		}
		
		private function initView():void
		{
			this.backgroundAuto = true;
			this.backgroundSkin = ResourceConst.BUILD_IMAGE_ITEM;
			
			nameLabel = TextFieldFactory.getInstance().getLabel("buildInterface_label_name");
//			nameLabel = new Label("建筑名称");
//			nameLabel.backgroundAuto = true;
			addChild(nameLabel);
			
			image = new UISprite();
			addChild(image);
			
			numberLabel = TextFieldFactory.getInstance().getLabel("buildInterface_label_stock");
//			numberLabel.backgroundAuto = true;
			addChild(numberLabel);
			
			goldContainer = new UIComponent();
			addChild(goldContainer);
			goldIcon = new UIComponent();
			goldIcon.backgroundAuto = true;
			goldIcon.backgroundSkin = ResourceConst.BUILD_ICON_GOLD;
			goldLabel = TextFieldFactory.getInstance().getLabel("buildInterface_label_price");
			goldLabel.autoSize = true;
			goldLabel.x = 25;
			goldContainer.addChild(goldIcon);
			goldContainer.addChild(goldLabel);
			
			metirialBox = new Box();
			metirialBox.autoSize = true;
			addChild(metirialBox);
			metirialBox.itemDistance = 10;
			
			stoneLabel = new IconLabel(ResourceConst.MAIN_KEY+ResourceConst.RESOURCEINFO_STONE_ICON);
			woodLabel = new IconLabel(ResourceConst.MAIN_KEY+ResourceConst.RESOURCEINFO_WOOD_ICON);
			ironLabel = new IconLabel(ResourceConst.MAIN_KEY+ResourceConst.RESOURCEINFO_IRON_ICON);
			metirialBox.addItem(stoneLabel);
			metirialBox.addItem(woodLabel);
			metirialBox.addItem(ironLabel);
			
			_resourceTipsContainer = new Box();
			_resourceTipsContainer.autoSize = true;
			_resourceTipsContainer.align = UIAlign.LAYOUT_VERTICAL;
		}
		
		private function initData():void
		{
			setView(nameLabel,40,6.35);
			nameLabel.textColor = 0xffffff;
			
			setView(numberLabel,105.15,33.25,50,50);
			
			setView(goldContainer,0,123.9);
			
			setView(metirialBox,0,148);
		}
		
		private function setView(obj:UIComponent, x:Number=0, y:Number=0, w:Number=0, h:Number=0):void
		{
			obj.backgroundAuto = true;
			obj.x = x;
			obj.y = y;
			if(w!=0)
			{
				if(obj.background)
				{					
					obj.background.width = w;
				}
				obj.width = w;
			}
			if(h!=0)
			{
				if(obj.background)
				{
					obj.background.height = h;					
				}
				obj.height = h;								
			}			
		}
		
		private function showTips():void
		{
			UIManager.getToolTipManager().showTips(BuildingTips,this,{data:_data.buildingObj},ToolTipManager.FOLLOW);
		}

		
		override public function onMouseOver():void
		{
			showTips();
		}
		
		override public function onRollOut():void
		{
//			hideTips();
			filters = [];
		}
		
		override public function onClick():void
		{
			trace(_data.objectName);
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
		}

		public function get data():Object
		{
			return _data;
		}

		/**
		 * 传入数据后，根据数据修改各个面板的数值信息 
		 * @param value 包含建筑信息的数据
		 */
		public function set data(value:Object):void
		{
			_data = value;
			var buildingObj:BuildingObj = value.buildingObj as BuildingObj;
			var userInfo:UserInfoObj = value.userInfo as UserInfoObj;
			if(!userInfo)
			{
				userInfo = new UserInfoObj();
				userInfo.gold = 20;
				userInfo.iron = 120;
				userInfo.wood = 120;
				userInfo.stone = 120;
			}
			
			nameLabel.text = UIManager.getLanguageManager().getText(buildingObj.objectName);
//			nameLabel.text = value.objectName;
			nameLabel.x = (width-nameLabel.width)/2;		
			numberLabel.text = "";
			if(userInfo.gold < buildingObj.commodityObj.gold)
			{
				_resourceEnoughArray[0] = false;
				_goldEnough = false;
				goldLabel.textColor = 0xff0000;
			}
			goldLabel.text = ""+buildingObj.commodityObj.gold;
//			goldContainer.width = goldLabel.x + goldLabel.textWidth;
			goldContainer.x = (background.width-goldLabel.x - goldLabel.textWidth)/2;
			if(userInfo.stone < buildingObj.commodityObj.stone)
			{
				_resourceEnoughArray[1] = false;
				_resourceEnough = false;
				stoneLabel.textColor = 0xff0000;
			}
			if(userInfo.wood < buildingObj.commodityObj.wood)
			{
				_resourceEnoughArray[2] = false;
				_resourceEnough = false;
				woodLabel.textColor = 0xff0000;
			}
			if(userInfo.iron < buildingObj.commodityObj.iron)
			{
				_resourceEnoughArray[3] = false;
				_resourceEnough = false;
				ironLabel.textColor = 0xff0000;
			}
			stoneLabel.text = ""+buildingObj.commodityObj.stone;
			woodLabel.text = ""+buildingObj.commodityObj.wood;
			ironLabel.text = ""+buildingObj.commodityObj.iron;
			metirialBox.x = (background.width - metirialBox.width)/2;

			image.removeAllChild();
			image.addChild((buildingObj as BuildingObj).getIcon(0,0,0,0,updateImage));
			updateImage();
//			canBuild = true;
			
			if(!canBuild){
				var font:DarwinFont = new DarwinFont("",25);
				_resourceTipsContainer.height = 0;
				if(!_goldEnough){
					var goldTips:Label = new Label("金币不足",font);
					_resourceTipsContainer.addItem(goldTips);
					_resourceTipsContainer.width = goldTips.textWidth;
					_resourceTipsContainer.height += 30;
				}
				if(!_resourceEnough){
					var resTips:Label = new Label("资源不足",font);
					_resourceTipsContainer.addItem(resTips);
					_resourceTipsContainer.width = resTips.textWidth;
					_resourceTipsContainer.height += 30;
				}			
				_resourceTipsContainer.x = (background.width - _resourceTipsContainer.width)/2;
				_resourceTipsContainer.y = (background.height - _resourceTipsContainer.height)/2;
			}
		}
		
		public function showResourceTips():void
		{
			if(!canBuild){
//				_resourceTipsContainer.visible = true;
				filters = [FilterConst.RED_INNER_GLOW_FILTER];
				addChild(_resourceTipsContainer);
				_resourceTipsContainer.alpha = 1;
				TweenLite.to(_resourceTipsContainer,2,{alpha:0,delay:1});
			}
		}
		
		private function updateImage():void
		{
			image.x = 65-image.width/2;
			image.y = goldContainer.y - image.height/2-40;
		}

		public function get index():int
		{
			return _index;
		}

		public function set index(value:int):void
		{
			_index = value;
		}
		
		public function dispose():void
		{
			
		}

		public function get resourceEnoughArray():Array
		{
			return _resourceEnoughArray;
		}

		public function get canBuild():Boolean
		{
			if(_goldEnough && _resourceEnough){
				return true;
			}else{
				return false;
			}
		}


	}
}